// Decompiled on 周六 2月 22 19:31:49 CST 2025 with Zomboid Decompiler v0.1.3 using Vineflower.
package zombie.ui;

import java.util.ArrayList;
import zombie.characters.IsoPlayer;
import zombie.core.Color;
import zombie.core.Core;
import zombie.core.textures.Texture;
import zombie.debug.DebugOptions;
import zombie.inventory.InventoryItem;
import zombie.inventory.types.AlarmClock;
import zombie.inventory.types.AlarmClockClothing;
import zombie.iso.weather.ClimateManager;
import zombie.network.GameClient;

public final class Clock extends UIElement {
    boolean bLargeTextures = false;
    Texture background = null;
    Texture[] digitsLarge;
    Texture[] digitsSmall;
    Texture colon = null;
    Texture slash = null;
    Texture minus = null;
    Texture dot = null;
    Texture tempC = null;
    Texture tempF = null;
    Texture tempE = null;
    Texture texAM = null;
    Texture texPM = null;
    Texture alarmOn = null;
    Texture alarmRinging = null;
    Color displayColour = new Color(100, 200, 210, 255);
    Color ghostColour = new Color(40, 40, 40, 128);
    int uxOriginal;
    int uyOriginal;
    int largeDigitSpacing;
    int smallDigitSpacing;
    int colonSpacing;
    int ampmSpacing;
    int alarmBellSpacing;
    int decimalSpacing;
    int degreeSpacing;
    int slashSpacing;
    int tempDateSpacing;
    int dateOffset;
    int minusOffset;
    int amVerticalSpacing;
    int pmVerticalSpacing;
    int alarmBellVerticalSpacing;
    int displayVerticalSpacing;
    int decimalVerticalSpacing;
    public boolean digital = false;
    public boolean isAlarmSet = false;
    public boolean isAlarmRinging = false;
    private IsoPlayer clockPlayer = null;
    public static Clock instance = null;

    public Clock(int x, int y) {
        this.x = (double)x;
        this.y = (double)y;
        instance = this;
    }

    @Override
    public void render() {
        if (this.visible) {
            this.assignTextures(Core.getInstance().getOptionClockSize() == 2);
            this.DrawTexture(this.background, 0.0, 0.0, 0.75);
            this.renderDisplay(true, this.ghostColour);
            this.renderDisplay(false, this.displayColour);
            super.render();
        }
    }

    private void renderDisplay(boolean _boolean, Color color) {
        int int0 = this.uxOriginal;
        int int1 = this.uyOriginal;

        for (int int2 = 0; int2 < 4; int2++) {
            int[] int3 = this.timeDigits();
            if (_boolean) {
                this.DrawTextureCol(this.digitsLarge[8], (double)int0, (double)int1, color);
            } else {
                this.DrawTextureCol(this.digitsLarge[int3[int2]], (double)int0, (double)int1, color);
            }

            int0 += this.digitsLarge[0].getWidth();
            if (int2 == 1) {
                int0 += this.colonSpacing;
                this.DrawTextureCol(this.colon, (double)int0, (double)int1, color);
                int0 += this.colon.getWidth() + this.colonSpacing;
            } else if (int2 < 3) {
                int0 += this.largeDigitSpacing;
            }
        }

        int0 += this.ampmSpacing;
        if (!Core.getInstance().getOptionClock24Hour() || _boolean) {
            if (_boolean) {
                this.DrawTextureCol(this.texAM, (double)int0, (double)(int1 + this.amVerticalSpacing), color);
                this.DrawTextureCol(this.texPM, (double)int0, (double)(int1 + this.pmVerticalSpacing), color);
            } else if (zombie.GameTime.getInstance().getTimeOfDay() < 12.0F) {
                this.DrawTextureCol(this.texAM, (double)int0, (double)(int1 + this.amVerticalSpacing), color);
            } else {
                this.DrawTextureCol(this.texPM, (double)int0, (double)(int1 + this.pmVerticalSpacing), color);
            }
        }

        if (this.isAlarmRinging || _boolean) {
            this.DrawTextureCol(
                this.alarmRinging, (double)(int0 + this.texAM.getWidth() + this.alarmBellSpacing), (double)(int1 + this.alarmBellVerticalSpacing), color
            );
        } else if (this.isAlarmSet) {
            this.DrawTextureCol(
                this.alarmOn, (double)(int0 + this.texAM.getWidth() + this.alarmBellSpacing), (double)(int1 + this.alarmBellVerticalSpacing), color
            );
        }

        if (this.digital || _boolean) {
            int0 = this.uxOriginal;
            int1 += this.digitsLarge[0].getHeight() + this.displayVerticalSpacing;
            if (this.clockPlayer == null) {
                int0 += this.dateOffset;
            } else {
                int[] int4 = this.tempDigits();
                if (int4[0] == 1 || _boolean) {
                    this.DrawTextureCol(this.minus, (double)int0, (double)int1, color);
                }

                int0 += this.minusOffset;
                if (int4[1] == 1 || _boolean) {
                    this.DrawTextureCol(this.digitsSmall[1], (double)int0, (double)int1, color);
                }

                int0 += this.digitsSmall[0].getWidth() + this.smallDigitSpacing;

                for (int int5 = 2; int5 < 5; int5++) {
                    if (_boolean) {
                        this.DrawTextureCol(this.digitsSmall[8], (double)int0, (double)int1, color);
                    } else {
                        this.DrawTextureCol(this.digitsSmall[int4[int5]], (double)int0, (double)int1, color);
                    }

                    int0 += this.digitsSmall[0].getWidth();
                    if (int5 == 3) {
                        int0 += this.decimalSpacing;
                        this.DrawTextureCol(this.dot, (double)int0, (double)(int1 + this.decimalVerticalSpacing), color);
                        int0 += this.dot.getWidth() + this.decimalSpacing;
                    } else if (int5 < 4) {
                        int0 += this.smallDigitSpacing;
                    }
                }

                int0 += this.degreeSpacing;
                this.DrawTextureCol(this.dot, (double)int0, (double)int1, color);
                int0 += this.dot.getWidth() + this.degreeSpacing;
                if (_boolean) {
                    this.DrawTextureCol(this.tempE, (double)int0, (double)int1, color);
                } else if (int4[5] == 0) {
                    this.DrawTextureCol(this.tempC, (double)int0, (double)int1, color);
                } else {
                    this.DrawTextureCol(this.tempF, (double)int0, (double)int1, color);
                }

                int0 += this.digitsSmall[0].getWidth() + this.tempDateSpacing;
            }

            int[] int6 = this.dateDigits();

            for (int int7 = 0; int7 < 4; int7++) {
                if (_boolean) {
                    this.DrawTextureCol(this.digitsSmall[8], (double)int0, (double)int1, color);
                } else {
                    this.DrawTextureCol(this.digitsSmall[int6[int7]], (double)int0, (double)int1, color);
                }

                int0 += this.digitsSmall[0].getWidth();
                if (int7 == 1) {
                    int0 += this.slashSpacing;
                    this.DrawTextureCol(this.slash, (double)int0, (double)int1, color);
                    int0 += this.slash.getWidth() + this.slashSpacing;
                } else if (int7 < 3) {
                    int0 += this.smallDigitSpacing;
                }
            }
        }
    }

    private void assignTextures(boolean _boolean) {
        if (this.digitsLarge == null || this.bLargeTextures != _boolean) {
            this.bLargeTextures = _boolean;
            if (_boolean) {
                this.background = Texture.getSharedTexture("media/ui/ClockAssets/ClockLargeBackground.png");
            } else {
                this.background = Texture.getSharedTexture("media/ui/ClockAssets/ClockSmallBackground.png");
            }

            String string0 = "Medium";
            String string1 = "Small";
            if (_boolean) {
                string0 = "Large";
                string1 = "Medium";
                this.assignLargeOffsets();
            } else {
                this.assignSmallOffsets();
            }

            if (this.digitsLarge == null) {
                this.digitsLarge = new Texture[10];
                this.digitsSmall = new Texture[10];
            }

            for (int _int = 0; _int < 10; _int++) {
                this.digitsLarge[_int] = Texture.getSharedTexture("media/ui/ClockAssets/ClockDigits" + string0 + _int + ".png");
                this.digitsSmall[_int] = Texture.getSharedTexture("media/ui/ClockAssets/ClockDigits" + string1 + _int + ".png");
            }

            this.colon = Texture.getSharedTexture("media/ui/ClockAssets/ClockDivide" + string0 + ".png");
            this.slash = Texture.getSharedTexture("media/ui/ClockAssets/DateDivide" + string1 + ".png");
            this.minus = Texture.getSharedTexture("media/ui/ClockAssets/ClockDigits" + string1 + "Minus.png");
            this.dot = Texture.getSharedTexture("media/ui/ClockAssets/ClockDigits" + string1 + "Dot.png");
            this.tempC = Texture.getSharedTexture("media/ui/ClockAssets/ClockDigits" + string1 + "C.png");
            this.tempF = Texture.getSharedTexture("media/ui/ClockAssets/ClockDigits" + string1 + "F.png");
            this.tempE = Texture.getSharedTexture("media/ui/ClockAssets/ClockDigits" + string1 + "E.png");
            this.texAM = Texture.getSharedTexture("media/ui/ClockAssets/ClockAm" + string0 + ".png");
            this.texPM = Texture.getSharedTexture("media/ui/ClockAssets/ClockPm" + string0 + ".png");
            this.alarmOn = Texture.getSharedTexture("media/ui/ClockAssets/ClockAlarm" + string0 + "Set.png");
            this.alarmRinging = Texture.getSharedTexture("media/ui/ClockAssets/ClockAlarm" + string0 + "Sound.png");
        }
    }

    private void assignSmallOffsets() {
        this.uxOriginal = 3;
        this.uyOriginal = 3;
        this.largeDigitSpacing = 1;
        this.smallDigitSpacing = 1;
        this.colonSpacing = 1;
        this.ampmSpacing = 1;
        this.alarmBellSpacing = 1;
        this.decimalSpacing = 1;
        this.degreeSpacing = 1;
        this.slashSpacing = 1;
        this.tempDateSpacing = 5;
        this.dateOffset = 33;
        this.minusOffset = 0;
        this.amVerticalSpacing = 7;
        this.pmVerticalSpacing = 12;
        this.alarmBellVerticalSpacing = 1;
        this.displayVerticalSpacing = 2;
        this.decimalVerticalSpacing = 6;
    }

    private void assignLargeOffsets() {
        this.uxOriginal = 3;
        this.uyOriginal = 3;
        this.largeDigitSpacing = 2;
        this.smallDigitSpacing = 1;
        this.colonSpacing = 3;
        this.ampmSpacing = 3;
        this.alarmBellSpacing = 5;
        this.decimalSpacing = 2;
        this.degreeSpacing = 2;
        this.slashSpacing = 2;
        this.tempDateSpacing = 8;
        this.dateOffset = 65;
        this.minusOffset = -2;
        this.amVerticalSpacing = 15;
        this.pmVerticalSpacing = 25;
        this.alarmBellVerticalSpacing = 1;
        this.displayVerticalSpacing = 5;
        this.decimalVerticalSpacing = 15;
    }

    private int[] timeDigits() {
        float float0 = zombie.GameTime.getInstance().getTimeOfDay();
        if (GameClient.bClient && GameClient.bFastForward) {
            float0 = zombie.GameTime.getInstance().ServerTimeOfDay;
        }

        if (!Core.getInstance().getOptionClock24Hour()) {
            if (float0 >= 13.0F) {
                float0 -= 12.0F;
            }

            if (float0 < 1.0F) {
                float0 += 12.0F;
            }
        }

        int int0 = (int)float0;
        float float1 = (float0 - (float)((int)float0)) * 60.0F;
        int int1 = int0 / 10;
        int int2 = int0 % 10;
        int int3 = (int)(float1 / 10.0F);
        return new int[]{int1, int2, int3, 0};
    }

    private int[] dateDigits() {
        int int0 = 0;
        int int1 = 0;
        int int2 = 0;
        int int3 = 0;
        int0 = (zombie.GameTime.getInstance().getDay() + 1) / 10;
        int1 = (zombie.GameTime.getInstance().getDay() + 1) % 10;
        int2 = (zombie.GameTime.getInstance().getMonth() + 1) / 10;
        int3 = (zombie.GameTime.getInstance().getMonth() + 1) % 10;
        return Core.getInstance().getOptionClockFormat() == 1 ? new int[]{int2, int3, int0, int1} : new int[]{int0, int1, int2, int3};
    }

    private int[] tempDigits() {
        float _float = ClimateManager.getInstance().getAirTemperatureForCharacter(this.clockPlayer, false);
        byte byte0 = 0;
        byte byte1 = 0;
        if (!Core.OptionTemperatureDisplayCelsius) {
            _float = _float * 1.8F + 32.0F;
            byte1 = 1;
        }

        if (_float < 0.0F) {
            byte0 = 1;
            _float *= -1.0F;
        }

        int int0 = (int)_float / 100;
        int int1 = (int)(_float % 100.0F) / 10;
        int int2 = (int)_float % 10;
        int int3 = (int)(_float * 10.0F) % 10;
        return new int[]{byte0, int0, int1, int2, int3, byte1};
    }

    public void resize() {
        this.visible = false;
        this.digital = false;
        this.clockPlayer = null;
        this.isAlarmSet = false;
        this.isAlarmRinging = false;
        if (IsoPlayer.getInstance() != null) {
            for (int int0 = 0; int0 < IsoPlayer.numPlayers; int0++) {
                IsoPlayer isoPlayer = IsoPlayer.players[int0];
                if (isoPlayer != null && !isoPlayer.isDead()) {
                    for (int int1 = 0; int1 < isoPlayer.getWornItems().size(); int1++) {
                        InventoryItem inventoryItem0 = isoPlayer.getWornItems().getItemByIndex(int1);
                        if (inventoryItem0 instanceof AlarmClock || inventoryItem0 instanceof AlarmClockClothing) {
                            this.visible = UIManager.VisibleAllUI;
                            this.digital = this.digital | inventoryItem0.hasTag("Digital");
                            if (inventoryItem0 instanceof AlarmClock) {
                                if (((AlarmClock)inventoryItem0).isAlarmSet()) {
                                    this.isAlarmSet = true;
                                }

                                if (((AlarmClock)inventoryItem0).isRinging()) {
                                    this.isAlarmRinging = true;
                                }
                            } else {
                                if (((AlarmClockClothing)inventoryItem0).isAlarmSet()) {
                                    this.isAlarmSet = true;
                                }

                                if (((AlarmClockClothing)inventoryItem0).isRinging()) {
                                    this.isAlarmRinging = true;
                                }
                            }

                            this.clockPlayer = isoPlayer;
                        }
                    }

                    if (this.clockPlayer != null) {
                        break;
                    }

                    ArrayList arrayList = isoPlayer.getInventory().getItems();

                    for (int int2 = 0; int2 < arrayList.size(); int2++) {
                        InventoryItem inventoryItem1 = (InventoryItem)arrayList.get(int2);
                        if (inventoryItem1 instanceof AlarmClock || inventoryItem1 instanceof AlarmClockClothing) {
                            this.visible = UIManager.VisibleAllUI;
                            this.digital = this.digital | inventoryItem1.hasTag("Digital");
                            if (inventoryItem1 instanceof AlarmClock) {
                                if (((AlarmClock)inventoryItem1).isAlarmSet()) {
                                    this.isAlarmSet = true;
                                }

                                if (((AlarmClock)inventoryItem1).isRinging()) {
                                    this.isAlarmRinging = true;
                                }
                            } else {
                                if (((AlarmClockClothing)inventoryItem1).isAlarmSet()) {
                                    this.isAlarmSet = true;
                                }

                                if (((AlarmClockClothing)inventoryItem1).isRinging()) {
                                    this.isAlarmRinging = true;
                                }
                            }

                            this.clockPlayer = isoPlayer;
                        }
                    }
                }
            }
        }

        if (DebugOptions.instance.CheatClockVisible.getValue()) {
            this.digital = true;
            this.visible = UIManager.VisibleAllUI;
        }

        if (this.background == null) {
            if (Core.getInstance().getOptionClockSize() == 2) {
                this.background = Texture.getSharedTexture("media/ui/ClockAssets/ClockLargeBackground.png");
            } else {
                this.background = Texture.getSharedTexture("media/ui/ClockAssets/ClockSmallBackground.png");
            }
        }

        this.setHeight((double)this.background.getHeight());
        this.setWidth((double)this.background.getWidth());
    }

    public boolean isDateVisible() {
        return this.visible && this.digital;
    }

    @Override
    public Boolean onMouseDown(double x, double y) {
        if (!this.isVisible()) {
            return false;
        } else {
            if (this.isAlarmRinging) {
                if (IsoPlayer.getInstance() != null) {
                    for (int int0 = 0; int0 < IsoPlayer.numPlayers; int0++) {
                        IsoPlayer isoPlayer0 = IsoPlayer.players[int0];
                        if (isoPlayer0 != null && !isoPlayer0.isDead()) {
                            for (int int1 = 0; int1 < isoPlayer0.getWornItems().size(); int1++) {
                                InventoryItem inventoryItem0 = isoPlayer0.getWornItems().getItemByIndex(int1);
                                if (inventoryItem0 instanceof AlarmClock) {
                                    ((AlarmClock)inventoryItem0).stopRinging();
                                } else if (inventoryItem0 instanceof AlarmClockClothing) {
                                    ((AlarmClockClothing)inventoryItem0).stopRinging();
                                }
                            }

                            for (int int2 = 0; int2 < isoPlayer0.getInventory().getItems().size(); int2++) {
                                InventoryItem inventoryItem1 = (InventoryItem)isoPlayer0.getInventory().getItems().get(int2);
                                if (inventoryItem1 instanceof AlarmClock) {
                                    ((AlarmClock)inventoryItem1).stopRinging();
                                } else if (inventoryItem1 instanceof AlarmClockClothing) {
                                    ((AlarmClockClothing)inventoryItem1).stopRinging();
                                }
                            }
                        }
                    }
                }
            } else if (this.isAlarmSet) {
                if (IsoPlayer.getInstance() != null) {
                    for (int int3 = 0; int3 < IsoPlayer.numPlayers; int3++) {
                        IsoPlayer isoPlayer1 = IsoPlayer.players[int3];
                        if (isoPlayer1 != null && !isoPlayer1.isDead()) {
                            for (int int4 = 0; int4 < isoPlayer1.getWornItems().size(); int4++) {
                                InventoryItem inventoryItem2 = isoPlayer1.getWornItems().getItemByIndex(int4);
                                if (inventoryItem2 instanceof AlarmClock && ((AlarmClock)inventoryItem2).isAlarmSet()) {
                                    ((AlarmClock)inventoryItem2).setAlarmSet(false);
                                } else if (inventoryItem2 instanceof AlarmClockClothing && ((AlarmClockClothing)inventoryItem2).isAlarmSet()) {
                                    ((AlarmClockClothing)inventoryItem2).setAlarmSet(false);
                                }
                            }

                            for (int int5 = 0; int5 < isoPlayer1.getInventory().getItems().size(); int5++) {
                                InventoryItem inventoryItem3 = (InventoryItem)isoPlayer1.getInventory().getItems().get(int5);
                                if (inventoryItem3 instanceof AlarmClockClothing && ((AlarmClockClothing)inventoryItem3).isAlarmSet()) {
                                    ((AlarmClockClothing)inventoryItem3).setAlarmSet(false);
                                }

                                if (inventoryItem3 instanceof AlarmClock && ((AlarmClock)inventoryItem3).isAlarmSet()) {
                                    ((AlarmClock)inventoryItem3).setAlarmSet(false);
                                }
                            }
                        }
                    }
                }
            } else if (IsoPlayer.getInstance() != null) {
                for (int int6 = 0; int6 < IsoPlayer.numPlayers; int6++) {
                    IsoPlayer isoPlayer2 = IsoPlayer.players[int6];
                    if (isoPlayer2 != null && !isoPlayer2.isDead()) {
                        for (int int7 = 0; int7 < isoPlayer2.getWornItems().size(); int7++) {
                            InventoryItem inventoryItem4 = isoPlayer2.getWornItems().getItemByIndex(int7);
                            if (inventoryItem4 instanceof AlarmClock && ((AlarmClock)inventoryItem4).isDigital() && !((AlarmClock)inventoryItem4).isAlarmSet()) {
                                ((AlarmClock)inventoryItem4).setAlarmSet(true);
                                if (this.isAlarmSet) {
                                    return true;
                                }
                            }

                            if (inventoryItem4 instanceof AlarmClockClothing
                                && ((AlarmClockClothing)inventoryItem4).isDigital()
                                && !((AlarmClockClothing)inventoryItem4).isAlarmSet()) {
                                ((AlarmClockClothing)inventoryItem4).setAlarmSet(true);
                                if (this.isAlarmSet) {
                                    return true;
                                }
                            }
                        }

                        for (int int8 = 0; int8 < isoPlayer2.getInventory().getItems().size(); int8++) {
                            InventoryItem inventoryItem5 = (InventoryItem)isoPlayer2.getInventory().getItems().get(int8);
                            if (inventoryItem5 instanceof AlarmClock && ((AlarmClock)inventoryItem5).isDigital() && !((AlarmClock)inventoryItem5).isAlarmSet()) {
                                ((AlarmClock)inventoryItem5).setAlarmSet(true);
                                if (this.isAlarmSet) {
                                    return true;
                                }
                            }

                            if (inventoryItem5 instanceof AlarmClockClothing
                                && ((AlarmClockClothing)inventoryItem5).isDigital()
                                && !((AlarmClockClothing)inventoryItem5).isAlarmSet()) {
                                ((AlarmClockClothing)inventoryItem5).setAlarmSet(true);
                                if (this.isAlarmSet) {
                                    return true;
                                }
                            }
                        }
                    }
                }
            }

            return true;
        }
    }
}
